﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using NCKH.models;
using NCKH;
namespace NCKH.Test
{
    public class TimeConstraints
    {
        public static void ConstraintBasicCompulsoryTime(XmlDocument xmlDoc, XmlNode root, string pathFile)
        {
            XmlNode nodeParent = xmlDoc.CreateNode(XmlNodeType.Element, "ConstraintBasicCompulsoryTime", null);
            {
                //-------------------------------------------------------
                XmlNode nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Weight_Percentage", null);
                nodeChild.InnerText = "100";
                nodeParent.AppendChild(nodeChild);
                //-------------------------------------------------------
                nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Active", null);
                nodeChild.InnerText = "true";
                nodeParent.AppendChild(nodeChild);
                //-------------------------------------------------------
                nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Comments", null);
                nodeParent.AppendChild(nodeChild);
            }
            root.AppendChild(nodeParent);
            xmlDoc.Save(pathFile);
        }
        public static void ConstraintBreakTimes(XmlDocument xmlDoc, XmlNode root, string pathFile)
        {
            Context db = new Context();

           // var rangBuoc = db.SCH_RangBuoc_TCs.FirstOrDefault(w => w.Ky_dang_ky == Program.DataSchedule.Ky_dang_ky && w.Ma_rang_buoc == "ConstraintBreakTimes");

            var xml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><CONSTRAINT><Time><Day>0</Day><Hour>0</Hour></Time><Time><Day>0</Day><Hour>2</Hour></Time></CONSTRAINT>";
            var index = Methods.Times.IndexOf("Nghỉ");

            if (xml != string.Empty || index != -1)
            {
                XmlNode nodeParent = xmlDoc.CreateNode(XmlNodeType.Element, "ConstraintBreakTimes", null);
                {
                    XmlNode nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Weight_Percentage", null);
                    nodeChild.InnerText = "100";
                    nodeParent.AppendChild(nodeChild);
                    //-------------------------------------------------------
                    XmlDocument xmlDocInput = new XmlDocument();
                    xmlDocInput.LoadXml(xml);

                    XmlNode xmlElement = xmlDocInput.DocumentElement;

                    for (int i = 0; i < xmlElement.ChildNodes.Count; i++)
                    {
                        XmlNode xmlNode = xmlElement.ChildNodes.Item(i);
                        //----------------------------------------------------
                        nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Break_Time", null);
                        {
                            XmlNode nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Day", null);
                            nodeLeaf.InnerText = Methods.Days[int.Parse(xmlNode.ChildNodes.Item(0).InnerText)];
                            nodeChild.AppendChild(nodeLeaf);
                            //----------------------------------------------------
                            nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Hour", null);
                            nodeLeaf.InnerText = Methods.Times[int.Parse(xmlNode.ChildNodes.Item(1).InnerText) == index ? int.Parse(xmlNode.ChildNodes.Item(1).InnerText) + 1 : int.Parse(xmlNode.ChildNodes.Item(1).InnerText)];
                            nodeChild.AppendChild(nodeLeaf);
                        }
                        nodeParent.AppendChild(nodeChild);
                    }

                    foreach (var day in Methods.Days)
                    {
                        nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Break_Time", null);
                        {
                            XmlNode nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Day", null);
                            nodeLeaf.InnerText = day;
                            nodeChild.AppendChild(nodeLeaf);
                            //----------------------------------------------------
                            nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Hour", null);
                            nodeLeaf.InnerText = Methods.Times[index];
                            nodeChild.AppendChild(nodeLeaf);
                        }
                        nodeParent.AppendChild(nodeChild);
                    }
                    //-------------------------------------------------------
                    nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Number_of_Break_Times", null);
                    nodeChild.InnerText = (xmlElement.ChildNodes.Count + (index != -1 ? Methods.Days.Count : 0)).ToString();
                    nodeParent.AppendChild(nodeChild);
                    //-------------------------------------------------------
                    nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Active", null);
                    nodeChild.InnerText = "true";
                    nodeParent.AppendChild(nodeChild);
                    //-------------------------------------------------------
                    nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Comments", null);
                    nodeParent.AppendChild(nodeChild);
                }
                root.AppendChild(nodeParent);
                xmlDoc.Save(pathFile);
            }
        }
    }
}